#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "cw.h"

struct scintwar
{
       char* name;
       Bool arr;
};

cintwar
cwa_create(char* name){
       cintwar cwa = NULL;
       cwa = (cintwar) calloc (1, sizeof (struct scintwar));
       if ( cwa == NULL){
               cwa = cwa_destroy (cwa);
               err(EXIT_FAILURE,ERRFMT,"cwa_create: No hay suficiente memoria\n",__LINE__);
       }
       cwa->name = (char*) calloc ( strlen( name) +1 , sizeof (char));
       if ( cwa->name == NULL){
               cwa = cwa_destroy (cwa);
               err(EXIT_FAILURE,ERRFMT,"cwa_create: No hay suficiente memoria\n",__LINE__);
       }
       strncpy(cwa -> name , name, strlen(name));
       cwa -> arr = FALSE;
       return cwa;
}

void
cwa_arr(cintwar cwa){
       if ( cwa == NULL){
               cwa = cwa_destroy (cwa);
               err(EXIT_FAILURE,ERRFMT,"cwa_arr: Cinta NULA\n",__LINE__);
       }
       printf (" graph %s { \n" , cwa -> name);
       cwa ->arr = TRUE;
}
void
cwa_insarbol(cintwar cwa, arista ar){
       if ( cwa == NULL){
               cwa = cwa_destroy (cwa);
               err(EXIT_FAILURE,ERRFMT,"cwa_insarbol: Cinta NULA\n",__LINE__);
       }
       if ((cwa -> arr)!= TRUE){
               cwa = cwa_destroy (cwa);
               err(EXIT_FAILURE,ERRFMT,"cwa_insarbol : La Cinta no fue arrancada\n",__LINE__);
       }
       if ( ar == NULL){
               warn(ERRFMT,"cwa_insarbol: Arista NULA\n",__LINE__);
       }else{
               printf (" %d -- %d [ label = %d ];\n",arista_v1(ar),arista_v2(ar),arista_peso(ar));
       }

}

void
cwa_insrest(cintwar cwa, arista ar){
       if ( cwa == NULL){
               cwa = cwa_destroy (cwa);
               err(EXIT_FAILURE,ERRFMT,"cwa_insarbol: Cinta NULA\n",__LINE__);
       }
       if ((cwa -> arr)!= TRUE){
               cwa = cwa_destroy (cwa);
               err(EXIT_FAILURE,ERRFMT,"cwa_insarbol : La Cinta no fue arrancada\n",__LINE__);
       }
       if ( ar == NULL){
               warn(ERRFMT,"cwa_insarbol: Arista NULA\n",__LINE__);
       }else{
               printf (" %d -- %d [ label = %d ,style=dotted];\n",arista_v1(ar),arista_v2(ar),arista_peso(ar));
       }
}
cintwar
cwa_destroy(cintwar cwa){
       
       printf ("}\n");
       if(cwa -> name != NULL){
               free (cwa -> name);
               cwa -> name = NULL;
       }
       
       free(cwa);
       cwa = NULL;
       return cwa ;
}
